package xyz.o0o0o.demo.AuthorizationServerDemo2.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.common.util.SerializationUtils;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

import com.alibaba.fastjson.JSON;

import xyz.o0o0o.demo.AuthorizationServerDemo2.dao.OAuthAccessTokenDao;
import xyz.o0o0o.demo.AuthorizationServerDemo2.entity.CustomOAuth2AccessToken;
import xyz.o0o0o.demo.AuthorizationServerDemo2.entity.CustomOAuth2RefreshToken;
import xyz.o0o0o.demo.AuthorizationServerDemo2.entity.OAuthAccessToken;
import xyz.o0o0o.demo.AuthorizationServerDemo2.service.CustomJdbcTokenStore;

public class CustomJdbcTokenStoreImpl implements CustomJdbcTokenStore {

	@Autowired
	private OAuthAccessTokenDao oAuthAccessTokenDao;

	public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
		System.out.println("CustomJdbcTokenStoreImpl readAuthentication");
		return null;
	}

	public OAuth2Authentication readAuthentication(String token) {
		System.out.println("CustomJdbcTokenStoreImpl readAuthentication");
		return null;
	}

	public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
		System.out.println("CustomJdbcTokenStoreImpl storeAccessToken");
		System.out.println("token : "+JSON.toJSON(token));
		System.out.println("authentication : "+JSON.toJSON(authentication));
		if (readAccessToken(token.getValue()) != null) {
			removeAccessToken(token);
		}

		oAuthAccessTokenDao.insert(new OAuthAccessToken(token, authentication));
	}

	public OAuth2AccessToken readAccessToken(String tokenValue) {
		System.out.println("CustomJdbcTokenStoreImpl readAccessToken");
		System.out.println("tokenValue :" + tokenValue);
		OAuthAccessToken token = oAuthAccessTokenDao.selectByTokenId(tokenValue);
		System.out.println("token : "+JSON.toJSON(token));
		if (token == null) {
			return null;
		} else {
			return new CustomOAuth2AccessToken(token);
		}
	}

	public void removeAccessToken(OAuth2AccessToken token) {
		System.out.println("CustomJdbcTokenStoreImpl removeAccessToken");
		System.out.println("token : "+JSON.toJSON(token));
		oAuthAccessTokenDao.delete(token.getValue());
	}

	public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
		System.out.println("CustomJdbcTokenStoreImpl readRefreshToken");

	}

	public OAuth2RefreshToken readRefreshToken(String tokenValue) {
		System.out.println("CustomJdbcTokenStoreImpl readRefreshToken");
		OAuthAccessToken token = oAuthAccessTokenDao.selectByTokenId(tokenValue);
		if (tokenValue == null) {
			return null;
		} else {
			return new CustomOAuth2RefreshToken(token.getRefresh_token());
		}
	}

	public OAuth2Authentication readAuthenticationForRefreshToken(OAuth2RefreshToken token) {
		System.out.println("CustomJdbcTokenStoreImpl readAuthenticationForRefreshToken");
		return null;
	}

	public void removeRefreshToken(OAuth2RefreshToken token) {
		System.out.println("CustomJdbcTokenStoreImpl removeRefreshToken");

	}

	public void removeAccessTokenUsingRefreshToken(OAuth2RefreshToken refreshToken) {
		System.out.println("CustomJdbcTokenStoreImpl removeAccessTokenUsingRefreshToken");

	}

	public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
		System.out.println("CustomJdbcTokenStoreImpl getAccessToken");
		System.out.println("authentication:"+JSON.toJSON(authentication));
		String ser = SerializationUtils.serialize(authentication).toString();
		OAuthAccessToken token = oAuthAccessTokenDao.select(ser);
		System.out.println("token:"+JSON.toJSON(token));
		if (token == null) {
			System.out.println(" null");
			return null;
		} else {
			System.out.println("not null");
			return new CustomOAuth2AccessToken(token);
		}
	}

	public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {
		System.out.println("CustomJdbcTokenStoreImpl findTokensByClientIdAndUserName");
		return null;
	}

	public Collection<OAuth2AccessToken> findTokensByClientId(String clientId) {
		System.out.println("CustomJdbcTokenStoreImpl findTokensByClientId");
		return null;
	}

}
